# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')
from os import listdir
from os.path import isfile, join

FLAGS = flags.FLAGS


def cal_invalid_book_ratio(book_df, full_symbol):
  invalid_count = len(book_df.loc[book_df['invalid'] == True])
  df_count = len(book_df)
  invalid_book_ratio = invalid_count / df_count * 100
  print(full_symbol, 'invalid book ratio: %.2f%%' % invalid_book_ratio)


def read_csv_into_df(csv_path):
  df = pandas.read_csv(csv_path, sep=',', header=0)
  df['datetime'] = pandas.to_datetime(df['timestamp'], unit='ns')
  df = df.set_index('datetime')
  return df


def enumerate_csv_files(csv_root):
  csv_files = [f for f in listdir(csv_root) if isfile(join(csv_root, f))]
  return csv_files


def main(argv):
  csv_root = FLAGS.csv_root
  assert csv_root, '--csv_root must be specified.'

  csv_files = enumerate_csv_files(csv_root)

  for csv_file in csv_files:
    full_symbol = '.'.join(csv_file.split('.')[:-1])
    book_df = read_csv_into_df(join(csv_root, csv_file))
    cal_invalid_book_ratio(book_df, full_symbol)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_root', None, 'Intput csv file path.')

  app.run(main)
